package com.ecom.common.controller;

import com.ecom.common.model.User;
import com.ecom.common.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by jimmy on 7/28/14.
 */


@Controller
@RequestMapping("/user")
public class LoginController {

    private static final Logger logger = Logger.getLogger(ProductController.class);

    @Autowired
    UserService userService;


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String init(ModelMap model) {

        User user = new User();

        model.addAttribute("user", user);

        return "login";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processLogin(@ModelAttribute("user") User user, BindingResult result) {

        logger.debug(" processLogin username is: " + user.getUsername());
        logger.debug(" processLogin password is: " + user.getPassword());

        if (userService.verifyUser(user)) {
            return "hello";
        } else {
            return "login";
        }

    }
}
